﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Data.Objects;

namespace Yelper.Web.Controllers {

    public class HomeController : Controller {

        public ActionResult Index() {
            if (Request.IsAuthenticated) {

                using (yelperEntities entities = new yelperEntities()) {
                   
                    var user = entities.Users.Where(u => u.Username == HttpContext.User.Identity.Name).SingleOrDefault();

                    // take 24 random idols and groupies (24 works well with the ui, rows of 6)
                    Random rnd = new Random();
                    ViewData["Idols"] = user.Idols.OrderBy(x => rnd.Next()).Take(24).ToList();
                    ViewData["Groupies"] = user.Groupies.OrderBy(x => rnd.Next()).Take(24).ToList();      

                    ViewData.Model = user.Idols.SelectMany(u => u.Yelps).Concat(user.Yelps). OrderByDescending(y => y.YelpDate).Take(20).ToList();
      
                }
                return View("IndexUser");

            }
            else {

                using (yelperEntities entities = new yelperEntities()) {

                    // Voor wouter: Toont de SQL achter de LINQ
                    // string debug = ((ObjectQuery)entities.Yelps.Include("User").OrderByDescending(x => x.YelpDate).Take(20)).ToTraceString();

                    ViewData.Model = entities.Yelps.Include("User").OrderByDescending(x => x.YelpDate).Take(20).ToList();

                }
                return View();

            }
        }

        public ActionResult Yelp(string username, int yelpid) {
            Yelp yelp;
            using (yelperEntities entities = new yelperEntities()) {
                User user = entities.Users.Where(x => x.Username==username).Single();
                yelp = entities.Yelps.Where(x => x.UserId == user.UserId && x.YelpId == yelpid).Single();
                ViewData["user"] = user;
            }
            return View(yelp);
        }
     
        public ActionResult UserPage(string username) {
            List<Yelp> timeline;
            using (yelperEntities entities = new yelperEntities()) {
                var users = entities.Users.Where(x => x.Username == username && x.Activated);
                if (users.Count() != 1) return RedirectToAction("NotFound","Errors");
                User user = users.Single();
                timeline = entities.Yelps.Include("User")
                    .Where(x => x.User.UserId == user.UserId)
                    .OrderByDescending(x => x.YelpDate)
                    .Take(20).ToList();
                ViewData["user"] = user;
                // take 24 random idols and groupies (24 works well with the ui, rows of 6)
                Random rnd = new Random();
                ViewData["Idols"] = user.Idols.OrderBy(x => rnd.Next()).Take(24).ToList();
                ViewData["Groupies"] = user.Groupies.OrderBy(x => rnd.Next()).Take(24).ToList();      
                if (Request.IsAuthenticated) {
                    string currentuser = User.Identity.Name;
                    bool IsIdol = entities.Users.Where(x => x.Username == currentuser).Single()
                        .Idols.Where(x => x.Username == username).Count() == 1;
                    ViewData["IsIdol"] = IsIdol;
                }
            }
            return View(timeline);
        }



        [Authorize]
        public ActionResult Mentions() {
            using (yelperEntities entities = new yelperEntities()) {

                var user = entities.Users.Where(u => u.Username == HttpContext.User.Identity.Name).SingleOrDefault();

                // take 24 random idols and groupies (24 works well with the ui, rows of 6)
                Random rnd = new Random();
                ViewData["Idols"] = user.Idols.OrderBy(x => rnd.Next()).Take(24).ToList();
                ViewData["Groupies"] = user.Groupies.OrderBy(x => rnd.Next()).Take(24).ToList();

                var mention = entities.Mentions.Include("Yelps.User").Where(m => m.Mention1 == HttpContext.User.Identity.Name).SingleOrDefault();

                var yelps = new List<Yelp>();


                if (mention != null) {
                    yelps = mention.Yelps.OrderByDescending(x => x.YelpDate).Take(20).ToList();

                }

                ViewData.Model = yelps;

            }


            return View();


        }
    }

}
